package com.heima.article.service.impl;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionsBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.feigns.behavior.BehaviorFeign;
import com.heima.model.article.dto.CollectionBehaviorDto;
import com.heima.model.article.mess.UpdateArticleMess;
import com.heima.model.article.pojo.ApCollection;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojo.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApCollectionsBehaviorServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection>implements ApCollectionsBehaviorService {

    @Autowired
    BehaviorFeign behaviorFeign;


    @Override
    public ResponseResult collect(CollectionBehaviorDto dto) {

        //检测参数
        if (dto.getEntryId()==null||((dto.getType()!=0&&dto.getType()!=1))){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询行为实体数据
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user == null ? null : user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"关联的实体数据不存在");
        }
        //根据操作类型,进行行为的操作
        if (dto.getType().intValue()==0){
            ApCollection one = this.getOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, apBehaviorEntry.getId()).eq(ApCollection::getArticleId, dto.getEntryId()));


            if (one!=null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"不能重复操作");
            }

            one = new ApCollection();
            one.setEntryId(apBehaviorEntry.getId());
            one.setArticleId(dto.getEntryId());
            one.setType((short)0);
            one.setCollectionTime(new Date());
        }else {
            this.remove(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId,apBehaviorEntry.getId()).eq(ApCollection::getArticleId,dto.getEntryId()));

        }

        //计算热点文章的消息封装
        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        updateArticleMess.setArticleId(dto.getEntryId());
        updateArticleMess.setAdd(dto.getOperation().intValue()==0?1:-1);

        kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, JSON.toJSONString(updateArticleMess));

        log.info("点赞行为 热点文章计算  消息已发送  消息内容 ===>{}",updateArticleMess);
        return ResponseResult.okResult();
    }


    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;
}
